package vip.xiaonuo.smzq.modular.kcl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.DirTree;
import vip.xiaonuo.smzq.modular.hisi.entity.vo.CxtjMhtj;
import vip.xiaonuo.smzq.modular.kcl.entity.Ks;
import vip.xiaonuo.smzq.modular.kcl.entity.param.XzqhCxtj;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.HomeRelated;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsCxtj;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsDetails;
import vip.xiaonuo.smzq.modular.kcl.service.IKsService;
import vip.xiaonuo.smzq.modular.psba.entity.vo.PsbaXqVo;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.KsAutocompleteVo;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿山基本信息
 *
 * @author eomer
 * @since 2024-02-22
 */
@Api(tags = "矿山基本信息")
@RestController
@RequestMapping("/kcl/ks")
@Validated
public class KsController {

    @Resource
    private IKsService iKsService;

    /**
     * 添加矿山基本信息
     */
    @ApiOperation("添加矿山基本信息")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestPart("files") MultipartFile files,@Validated  Ks ks) {
        iKsService.insertKs(ks,files);
        return CommonResult.ok();
    }

    /**
     * 添加矿山基本信息及其关联
     */
    @ApiOperation("添加矿山基本信息及其关联")
    @PostMapping("/addKsDetails")
    public CommonResult<String> addKsDetails(@RequestBody @Valid KsDetails ksDetails) {
        return CommonResult.data(iKsService.addKsDetails(ksDetails));
    }

    /**
     * 保存矿山基本信息及其关联
     */
    @ApiOperation("保存矿山基本信息及其关联")
    @PostMapping("/saveKsDetails")
    public CommonResult<String> saveKsDetails(@RequestBody @Valid KsDetails ksDetails) {
        return CommonResult.data(iKsService.saveKsDetails(ksDetails));
    }

    /**
     * 删除矿山基本信息
     */
    @ApiOperation("删除矿山基本信息")
    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody @Valid Ks ks) {
        iKsService.deleteKs(ks);
        return CommonResult.ok();
    }

    /**
     * 编辑矿山基本信息
     */
    @ApiOperation("编辑矿山基本信息")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Ks ks) {
        iKsService.updateById(ks);
        return CommonResult.ok();
    }

    /**
     * 矿山基本信息分页查询
     */
    @ApiOperation("矿山基本信息分页查询")
    @PostMapping("/getKspage")
    public CommonResult<Page<KsCxtj>> getKspage(
            @RequestBody @Valid CxtjMhtj cxtjMhtj
    ) {
        return CommonResult.data(iKsService.getKspage(cxtjMhtj));
    }

    /**
     * 矿山基本信息目录树
     */
    @ApiOperation("矿山基本信息目录树")
    @GetMapping("/ksDirTreelist")
    public CommonResult<List<DirTree>> ksDirTreelist(
            @ApiParam(name = "xzqhDm", value = "行政区划节点代码", required = true) @RequestParam Integer xzqhDm,
            @ApiParam(name = "nd", value = "年度", required = true) @RequestParam Integer nd
    ) {
        return CommonResult.data(iKsService.ksDirTreelist(xzqhDm, nd));
    }

    /**
     * 矿山基本信息目录树-添加搜索条件
     */
    @ApiOperation("矿山基本信息目录树-添加搜索条件")
    @PostMapping ("/ksDirTreelistNew")
    public CommonResult<List<DirTree>> ksDirTreelistNew(
            @RequestBody @Valid XzqhCxtj xzqhCxtj
    ) {
        return CommonResult.data(iKsService.ksDirTreelistNew(xzqhCxtj));
    }


    /**
     * 矿山基本信息及其关联
     */
    @ApiOperation("矿山基本信息及其关联")
    @GetMapping("/getKsDetails")
    public CommonResult<KsDetails> getKsDetails(
            @ApiParam(name = "nd", value = "年度", required = true) @RequestParam Integer nd,
            @ApiParam(name = "kqbm", value = "矿区编码", required = true) @RequestParam String kqbm,
             Integer djflbm
    ) {
        return CommonResult.data(iKsService.getKsDetails(nd, kqbm, djflbm));
    }

    /**
     * 根据许可证号获取矿山基本信息及其关联
     *
     * @Param 许可证号 xkzh
     */
    @ApiOperation("根据许可证号获取矿山基本信息及其关联")
    @GetMapping("/getKs/{xkzh}")
    public CommonResult<PsbaXqVo> getKs(
            @ApiParam(name = "xkzh", value = "许可证号", required = true) @PathVariable("xkzh") String xkzh
    ) {
        return CommonResult.data(iKsService.getKs(xkzh));
    }

    /**
     * 数据同步，下一年的年报任务生成
     * 年度 nd
     * 矿区编号 kqbh
     * 登记分类编号 djflbh
     */
    @ApiOperation("数据同步，下一年的年报任务生成")
    @GetMapping("/generateAnnualReportTask")
    public CommonResult<Boolean> generateAnnualReportTask(
            @ApiParam(name = "nd", value = "年度", required = true) @RequestParam Integer nd,
            @ApiParam(name = "kqbm", value = "矿区编码", required = true) @RequestParam String kqbm,
            @ApiParam(name = "djflbm", value = "登记分类编码", required = true) @RequestParam Integer djflbm
    ) {
        return CommonResult.data(iKsService.generateAnnualReportTask(nd, kqbm, djflbm));
    }

    /**
     * 审核列表
     */
    @ApiOperation("审核列表")
    @GetMapping("/auditList")
    public CommonResult<Page<Ks>> auditList() {
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
        return CommonResult.data(iKsService.auditList(user));
    }

    /**
     * 获取首页详情
     */
    @ApiOperation("获取首页详情")
    @GetMapping("/getHomeRelated")
    public CommonResult<HomeRelated> getHomeRelated(
            Integer nd
    ) {

        return CommonResult.data(iKsService.getHomeRelated(nd));
    }

    /**
     * @Author yxt
     * @Description 矿山提交审核
     * @Date 2024/2/29 17:07
     **/
    @ApiOperation("矿山提交审核")
    @PostMapping("/submitAudit")
    public CommonResult<String> submitAudit(@ApiParam(name = "ksId", value = "矿山id", required = true) @RequestParam String ksId) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        iKsService.submitAudit(ksId, loginUser);
        return CommonResult.ok();
    }

    /**
     * @Author yxt
     * @Description 矿山信息审核
     * @Date 2024/2/29 17:07
     **/
    @ApiOperation("矿山信息审核")
    @PostMapping("/audit")
    public CommonResult<String> audit(@ApiParam(name = "ksId", value = "矿山id", required = true) @RequestParam String ksId,
                                      @ApiParam(name = "result", value = "结果", required = true) @RequestParam Integer result,
                                      @ApiParam(name = "opinions", value = "意见") @RequestParam(required = false) String opinions) {
        SaBaseLoginUser loginUser = StpLoginUserUtil.getLoginUser();
        iKsService.audit(ksId, result, opinions, loginUser);
        return CommonResult.ok();
    }


    @ApiOperation("获取自动填充矿山信息")
    @PostMapping("/autocompleteList")
    public CommonResult<List<KsAutocompleteVo>> autocompleteKsList(@ApiParam(name = "ksmc", value = "矿山名称") @RequestParam(required = false) String ksmc,
                                                                   @ApiParam(name = "nd", value = "年度") @RequestParam(required = false) Integer nd,
                                                                   @ApiParam(name = "flag",value="探采标识" ,example="1,2,3,4" ) @RequestParam(required = false) String flag) {
        return CommonResult.data(iKsService.autocompleteKsList(ksmc, nd,flag));
    }


    /**
     * 储量查询-过滤行政区划目录树
     * @Param xzqhCxtj
     */
    @ApiOperation("储量查询-过滤行政区划目录树")
    @PostMapping("/filterXzqh")
    public CommonResult<List<String>> filterXzqh(
            @RequestBody @Valid XzqhCxtj xzqhCxtj
    ){
        return CommonResult.data(iKsService.filterXzqh(xzqhCxtj));
    }
}
